#ifndef __FPGA_FILTER_H__
#define __FPGA_FILTER_H__

#include "graph_fpga.h"

#include <stdio.h>
#include<hls_math.h>
void tupleFilter(
    hls::stream<edge_tuples_t_decode>    &toFilterItem,
    hls::stream<int2_token>     &buildArray
)
{
#pragma HLS function_instantiate variable=toFilterItem

	edge_tuples_t_decode filter;
    ap_uint<1> filter_flag;
    uint filter_num;
    while (true) {
#pragma HLS PIPELINE II=1
#pragma HLS dependence variable=filter inter false
#pragma HLS dependence variable=buildArray inter false
#pragma HLS dependence variable=filter_num inter false


        read_from_stream(toFilterItem, filter);
        filter_flag = filter.flag;
        filter_num = filter.num;

        for (int j = 0; j < filter_num; j ++) {
#pragma HLS PIPELINE II=1 rewind
            {
                int2_token token;
                //token.data = filter.data[j];
                token.data.x=filter.datax.range(j*X_WIDTH+X_WIDTH-1,j*X_WIDTH);
                token.data.y=filter.datay.range(j*Y_WIDTH+Y_WIDTH-1,j*Y_WIDTH);
                token.flag = (j == (filter_num - 1 ))? filter.flag: (ap_uint<1>)0;
                write_to_stream(buildArray, token);
                //printf("filter:flag=%d\n",(int)token.flag);
                //printf("576 hit dst=%d, val=%d\n",(int)(token.data.x),(int)token.data.y);
            }
        }
        if (filter_flag)
        {
            break;
        }

    }
    //clear_stream(toFilterItem);
    return;
}
#endif /* __FPGA_FILTER_H__ */

